package org.example.exception;

import org.example.entity.AjaxResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = LoginException.class)
    public AjaxResult handleLoginException(LoginException ex) {
        return AjaxResult.err(401, ex.getMessage());
    }

//    @ExceptionHandler(value = NoPermsException.class)
//    public AjaxResult handleLoginException(NoPermsException ex) {
//        return AjaxResult.err(403, ex.getMessage());
//    }BadCredentialsException

    @ExceptionHandler(value = Exception.class)
    public AjaxResult handleLoginException(Exception ex) {
        ex.printStackTrace();
        return AjaxResult.err(ex.getMessage());
    }

    @ExceptionHandler(value = BadCredentialsException.class)
    public AjaxResult handleLoginException(BadCredentialsException ex) {
        return AjaxResult.err(401, "用户名或密码不正确");
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    public AjaxResult handleLoginException(AccessDeniedException ex) {
        return AjaxResult.err(403,"你不能访问这里");
    }
}
